#include "nationalite.h"
#include "bdd.h"
#include "options.h"
#include <QDialog>
#include <QListWidget>
#include <QPushButton>
#include <QVBoxLayout>

nationalite::nationalite()
{
    idNationalite = -1;
    codeAllocineNationalite = -1;
    nomNationalite = "";
}

int nationalite::Existe(int codeAllocine)
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idNationalite FROM nationalite WHERE codeAllocineNationalite ="
            + db.TexteVersSql(QString::number(codeAllocine));
    return db.EstDansLaBase(requete);
}

void nationalite::Enregistre()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "INSERT INTO nationalite(idNationalite, nomNationalite, codeAllocineNationalite) VALUES("
            "NULL, "
            +db.TexteVersSql(this->nomNationalite)+ ", "
            +db.TexteVersSql(QString::number(this->codeAllocineNationalite))+")";
    db.Execute(requete);
}

void nationalite::Supprime()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "DELETE FROM nationalite WHERE idNationalite="+db.TexteVersSql(QString::number(this->idNationalite));
    db.Execute(requete);
}

QList<nationalite> nationalite::ListeNationalite()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idNationalite, nomNationalite,codeAllocineNationalite FROM nationalite order by nomNationalite";
    db.Execute(requete);

    QList<nationalite> listeNat;

    while(db.Requete().next())
    {
        nationalite uneNationalite = nationalite(/*dossier::toNativeSeparators(db.Requete().value(1).toString())*/);
        uneNationalite.setId(db.Requete().value(0).toInt());
        uneNationalite.setNom(db.Requete().value(1).toString());
        uneNationalite.setCodeAllocine(db.Requete().value(2).toInt());
        listeNat << uneNationalite;

    }
    return listeNat;
}

void nationalite::setId(int id)
{
    idNationalite = id;
}

int nationalite::id()
{
    return this->idNationalite;
}

void nationalite::setCodeAllocine(int code) // != id : code Allocine
{
    this->codeAllocineNationalite = code;
}

int nationalite::codeAllocine()
{
    return this->codeAllocineNationalite;
}

void nationalite::setNom(QString nom)
{
    this->nomNationalite = nom;
}

QString nationalite::nom()
{
    return this->nomNationalite;
}

nationalite nationalite::dialogSelectNationalite()
{
    QDialog* dlg = new QDialog();
    QWidget* w = dlg;
    QListWidget* list = new QListWidget(w);
    QPushButton* bouton = new QPushButton("Annuler",w);
    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(list);
    layout->addWidget(bouton);
    w->setLayout(layout);

    dlg->connect(list,SIGNAL(doubleClicked(QModelIndex)),dlg,SLOT(accept()));
    dlg->connect(bouton,SIGNAL(clicked()),dlg,SLOT(reject()));

    QList<nationalite> listeNat = nationalite::ListeNationalite();
    nationalite n;
    foreach (n, listeNat)
    {
        QListWidgetItem* it = new QListWidgetItem( list,n.id());
        it->setText(n.nom());
        it->setData(998,n.codeAllocine());
        it->setData(999,n.id());
        list->addItem(it);
    }
    nationalite retour;
    if(dlg->exec())
    {
        QListWidgetItem* it  = list->currentItem();
        if(it == NULL)
            return retour;
        retour.setId(it->type());
        retour.setNom(it->text());
        retour.setCodeAllocine(it->data(998).toInt());
    }
    return retour;
}
